package hs.bank.Web;

import hs.bank.Data.CardMapper;
import hs.bank.Data.RoleMapper;
import hs.bank.Data.UserMapper;
import hs.bank.Data.User_Role_Mapper;
import hs.bank.Entity.Card;
import hs.bank.Entity.User;
import hs.bank.Entity.User_Role;
import hs.bank.Web.Form.RegistrationForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

@Controller
@RequestMapping("/registration")
public class RegistrationController {
    private UserMapper userMapper;
    private CardMapper cardMapper;
    private RoleMapper roleMapper;
    private User_Role_Mapper user_role_mapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Autowired
    public void setCardMapper(CardMapper cardMapper) {
        this.cardMapper = cardMapper;
    }

    @Autowired
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    @Autowired
    public void setUser_role_mapper(User_Role_Mapper user_role_mapper) {
        this.user_role_mapper = user_role_mapper;
    }

    @GetMapping
    public String showForm(Model model) {
        model.addAttribute("registrationForm", new RegistrationForm());
        return "registration";
    }

    @PostMapping
    public String register(@Valid RegistrationForm form, BindingResult bindingResult, Model model) {
        if (bindingResult.hasErrors()) {
            return "registration";
        }
        User user = userMapper.findByUsername(form.getUsername());
        if (user != null) {
            model.addAttribute("error", true);
            return "registration";
        }

        int newCardNumber = cardMapper.getCount() == 0 ? 1 : cardMapper.maxCardNumber() + 1;
        int newUserId = userMapper.getCount() == 0 ? 1 : userMapper.maxUserId() + 1;

        try {
            // save the new user
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String passwordEncrypt = bCryptPasswordEncoder.encode(form.getPassword());
            User newUser = new User();
            newUser.setUserId(newUserId);
            newUser.setPassword(passwordEncrypt);
            newUser.setUsername(form.getUsername());
            userMapper.save(newUser);

            // save new card
            Card card = new Card(newCardNumber, newUserId);
            cardMapper.save(card);

            // save new user_role
            int normalUserRoleId = roleMapper.normalUserRoleId();
            User_Role user_role = new User_Role(newUserId, normalUserRoleId);
            user_role_mapper.save(user_role);

            // saved all elements
            return "redirect:/auth/login";

        } catch (Exception e) {
            e.printStackTrace();
            return "registration";
        }

    }

}
